<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>订单支付</title>
    <link rel="stylesheet" type="text/css" href="../../../css/aui/aui.css" />
    <link rel="stylesheet" type="text/css" href="../../../css/html/issued/post_your_want_frm.css" />
    <style>
        .row_flex {
            display: flex;
            justify-content: space-between;
        }

        .list_item {
            padding: 0.5rem 0.75rem;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
        }

        .pay_icon {
            display: flex;
            align-items: center;
        }

        .pay_icon img {
            width: 1rem;
            height: 1rem;
            margin-right: 0.5rem;
        }

        .aui-dialog-body input {
            text-align: left;
        }

        [v-clock] {
            display: none;
        }
    </style>
</head>

<body>
    <div id="app" v-clock>
        <div class="aui-content aui-margin-b-15">
            <ul class="aui-list aui-form-list">
                <div class="row_flex" style="padding:0.5rem 0.75rem;">
                    <div>总计金额</div>
                    <div style="color:#F52E2E;">￥{{info.pay_price}}</div>
                </div>
            </ul>

            <div style="border-top:0.5rem solid #eee;border-bottom:0.5rem solid #eee;">
                <div class="list_item">
                    <div class="pay_icon"><img src="../../../image/icon/weixin.png" alt="">微信支付</div>
                    <input class="aui-radio" type="radio" name="pay_type" v-model="info.pay_types" value="2">
                </div>
                <div class="list_item">
                    <div class="pay_icon"><img src="../../../image/icon/zhifubao.png" alt="">支付宝支付</div>
                    <input class="aui-radio" type="radio" name="pay_type" v-model="info.pay_types" value="3">
                </div>
                <div class="list_item">
                    <div class="pay_icon"><img src="../../../image/icon/yue.png" alt="">余额支付</div>
                    <div>￥{{userInfo.wallet_balance}}<input class="aui-radio" type="radio" name="pay_type" v-model="info.pay_types" value="1"></div>
                </div>
            </div>
        </div>
    </div>
</body>
<script src="../../../script/api.js" charset="utf-8"></script>
<script src="../../../script/aui/aui-tab.js" charset="utf-8"></script>
<script src="../../../script/aui/aui-dialog.js" charset="utf-8"></script>
<script src="../../../script/common/jquery-3.3.1.min.js" charset="utf-8"></script>
<script src="../../../script/common/common.js" charset="utf-8"></script>
<script src="../../../script/common/config.js" charset="utf-8"></script>
<script src="../../../script/common/vue.min.js" charset="utf-8"></script>
<script>
    apiready = function() {
        api.parseTapmode();
        vm.init();
    }

    var vm = new Vue({
        el: "#app",
        data: {
            userInfo: {},
            info: {
                order_id: '',
                drawee_user_id: '',
                payee_user_id: '',
                pay_price: '',
                pay_types: 1,
                pay_pass: 0
            }
        },
        methods: {
            init() {
                this.userInfo = get_local('userInfo');
                this.gerUserInfo();
                this.info.order_id = api.pageParam.order_id;
                this.info.drawee_user_id = api.pageParam.drawee_user_id;
                this.info.payee_user_id = api.pageParam.payee_user_id;
                this.info.pay_price = api.pageParam.order_price;

            },
            gerUserInfo() {
                var that = this;
                var userInfo = get_local('userInfo');
                // 获取用户信息--信息不完善
                _ajax(Config.api.UserGetUser, {
                    user_id: userInfo.user_id
                }, function(ret) {
                    if (ret.code === 200) {
                        ret.data.user_id = userInfo.user_id;
                        that.userInfo = ret.data;
                        set_local('userInfo', that.userInfo);
                        that.userInfo = get_local('userInfo');
                    }
                });
            },
            pay() {
                if (this.info.pay_types == 1 && (this.userInfo.wallet_balance * 100 < this.info.order_price * 100)) {
                    $toast('余额不足');
                    return false;
                }
                if (this.info.pay_types == 1) {
                    var self = this;
                    var dialog = new auiDialog({});
                    dialog.prompt({
                        title: "支付密码",
                        text: '请输入支付密码',
                        type: 'password',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) {
                            if (ret.text.length != 6) {
                                $toast('请输入六位数密码');
                                return false;
                            }
                            self.info.pay_pass = ret.text;
                            self.addOrder();
                        }
                    })
                } else {
                    this.info.pay_pass = 0;
                    this.addOrder();
                }
            },
            addOrder() {
                var that = this;
                var countPrice = api.pageParam.order_price;
                if (this.info.pay_types == 1 && (this.userInfo.wallet_balance * 100 < countPrice * 100)) {
                    $toast("余额不足");
                    return;
                } else {
                    _ajax(Config.api.TransferGoPay, this.info, function(ret) {
                        if (ret.code === 200) {
                            if (that.info.pay_types == 1) {
                                api.execScript({
                                    name: 'main',
                                    frameName: 'my',
                                    script: 'vm.gerUserInfo()'
                                });
                                that.apySuccess();
                            } else if (that.info.pay_types == 2) {
                                that.wxPay(ret.data);
                            } else if (that.info.pay_types == 3) {
                                that.aliPay(ret.data.orderInfo);
                            }
                        } else {
                            $toast(ret.msgs);
                        }
                    });
                }
            },
            apySuccess() {
                $toast("支付成功");
                // 重新获取订单列表
                api.execScript({
                    name: 'offlineOrders_win',
                    frameName: 'offlineOrders_frm',
                    script: 'vm.getList()'
                });
                setTimeout(function() {
                    api.closeToWin({
                        name: 'offlineOrders_win'
                    });
                }, 1000);
            },
            wxPay(data) {
                var that = this;
                var wxPay = api.require('wxPay');
                wxPay.payOrder(data, function(ret, err) {
                    if (ret.status) {
                        that.apySuccess();
                    } else {
                        $toast('支付失败，请尝试其他方式支付' + err.code);
                    }
                });
            },
            aliPay(orderInfo) {
                var that = this;
                var aliPay = api.require('aliPay');
                aliPay.payOrder({
                    orderInfo: orderInfo
                }, function(ret, err) {
                    if (ret.code == 9000) {
                        that.apySuccess();
                    } else {
                        $toast('支付失败，请尝试其他方式支付' + ret.code);
                    }
                });
            },

        }
    });
</script>

</html>
